<template>
  <a-modal
      title="赠课审核"
      :visible="open"
      :confirm-loading="confirmLoading"
      @ok="submitForm"
      @cancel="open = false"
    >
      <a-form-model ref="form" :model="form" :rules="rules">
        <a-form-model-item label="状态" prop="status" >
          <a-radio-group v-model="form.status" button-style="solid">
            <a-radio-button value="2">审核通过</a-radio-button>
            <a-radio-button value="3">审核不通过</a-radio-button>
          </a-radio-group>
        </a-form-model-item>
        <a-form-model-item label="备注" prop="remark" >
          <a-input v-model="form.remark" placeholder="请输入内容" type="textarea" allow-clear />
        </a-form-model-item>
        <div class="bottom-control">
          <a-space>
            <a-button type="primary" :loading="submitLoading" @click="submitForm">
              保存
            </a-button>
            <a-button type="dashed" @click="cancel">
              取消
            </a-button>
          </a-space>
        </div>
      </a-form-model>
  </a-modal>
</template>

<script>
import { getApply, addApply, updateApply } from '@/api/manager/apply'

export default {
  name: 'CreateForm',
  props: {
  },
  components: {
  },
  data () {
    return {
      submitLoading: false,
      formTitle: '',
      // 表单参数
      form: {
        id: null,

        status: '2',

        phone: null,

        deptId: null,

        url: null,

        createBy: null,

        createTime: null,

        updateBy: null,

        updateTime: null,

        remark: null

      },
      // 1增加,2修改
      formType: 1,
      open: false,
      rules: {
      }
    }
  },
  filters: {
  },
  created () {
  },
  computed: {
  },
  watch: {
  },
  mounted () {
  },
  methods: {
    onClose () {
      this.open = false
    },
    // 取消按钮
    cancel () {
      this.open = false
      this.reset()
    },
    // 表单重置
    reset () {
      this.formType = 1
      this.form = {
        id: null,

        status: '0',

        phone: null,

        deptId: null,

        url: null,

        createBy: null,

        createTime: null,

        updateBy: null,

        updateTime: null,

        remark: null

      }
    },
    /** 新增按钮操作 */
    handleAdd (row) {
      this.reset()
      this.formType = 1
      this.open = true
      this.formTitle = '添加'
    },
    /** 修改按钮操作 */
    handleUpdate (row, ids) {
      if(row.status > 1){
        this.$message.error('该学员已完成审核！');
        return ;
      }

      this.reset()
      this.formType = 2
      const id = row ? row.id : ids
      getApply(id).then(response => {
        this.form = response.data
        this.form.status = '2';
        this.open = true
      })
    },
    /** 提交按钮 */
    submitForm: function () {
      this.$refs.form.validate(valid => {
        if (valid) {
          this.submitLoading = true
          if (this.form.id !== undefined && this.form.id !== null) {
            updateApply(this.form).then(response => {
              this.$message.success(
                '修改成功',
                3
              )
              this.open = false
              this.$emit('ok')
            }).finally(() => {
              this.submitLoading = false
            })
          } else {
            addApply(this.form).then(response => {
              this.$message.success(
                '新增成功',
                3
              )
              this.open = false
              this.$emit('ok')
            }).finally(() => {
              this.submitLoading = false
            })
          }
        } else {
          return false
        }
      })
    }
  }
}
</script>
